package cn.wolfcode.web.controller;

import cn.wolfcode.domain.Permission;
import cn.wolfcode.qo.JsonResult;
import cn.wolfcode.qo.PageResult;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.IPermissionService;
import cn.wolfcode.util.RequirePermission;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author L5775
 */
@Controller
@RequestMapping("/permission")
public class PermissionController {

    @Autowired
    private IPermissionService permissionService;

    // 处理查询请求
    // localhost/permission/list.do
    // JSP 查询的数据存到 Model 对象
    @RequestMapping("/list")
    @RequirePermission(name="权限页面",expression = "permission:list")
    public String list(Model model, QueryObject qo) {
        PageResult<Permission> pageResult = permissionService.query(qo);
        model.addAttribute("pageResult", pageResult);
        return "permission/list";
    }

    // 处理删除请求
    // http://localhost/permission/delete.do?id=1
    // 删除完了还希望浏览器再发送请求访问 localhost/permission/list.do
    @RequestMapping("/delete")
    @ResponseBody
    @RequirePermission(name="权限删除",expression = "permission:delete")
    public JsonResult delete(Long id) {
        try {
            if (id != null) {
                permissionService.delete(id);
            }
            return new JsonResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonResult(false, "系统异常,请联系管理员");
        }
    }
    @RequestMapping("/reload")
    @ResponseBody
    public JsonResult reload(){
        try {
            permissionService.reload();
            return new JsonResult();
        }catch (Exception e){
            e.printStackTrace();
            return new JsonResult(false,"系统异常,请联系管理员");
        }
    }
}
